package org.blogwu.learn.arithmetic.demo1;

public class Test {
    public static void main(String[] args) {
        {
            int[] i1 = {1, 3, 5, 6};
            System.out.println(searchInsert(i1, 5));
        }
        {
            int[] i2 = {1, 3, 5, 6};
            System.out.println(searchInsert(i2, 2));
        }
        {
            int[] i2 = {1, 3, 5, 6};
            System.out.println(searchInsert(i2, 7));
        }
        {
            int[] i2 = {1, 3, 5, 6};
            System.out.println(searchInsert(i2, 0));
        }
        {
            int[] i2 = {1};
            System.out.println(searchInsert(i2, 1));//0
        }
        {
            int[] i2 = {1, 3};
            System.out.println(searchInsert(i2, 3));//1
        }
    }

    public static int searchInsert(int[] nums, int target) {
        int c = nums.length;
        for (int i = c - 1; i >= 0; i--) {
            if (target > nums[i]) {
                return i + 1;
            } else if (target == nums[i]) {
                return i;
            }
        }
        return 0;
    }

}
